package com.arm.service.impl;

import com.arm.module.Order;
import com.arm.module.PayParameter;
import com.arm.module.Payment;
import com.arm.service.OrderService;
import com.arm.service.PayBase;
import com.arm.service.PayService;
import com.arm.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2019-1-31.
 */
@Service("backGroundPayService")
public class BackGroundPayServiceImpl extends PayBase implements PayService {

    @Autowired
    private OrderService orderService;
    @Autowired
    PaymentService paymentService;

    @Override
    public PayParameter pay(String orderId, String price, String remoteAddr, Map<String, String> payParametersMap) throws Exception {
        Order order = orderService.getOrderById(Long.parseLong(orderId));
        PayParameter payParameter = new PayParameter();

        List<Payment> paymentList = order.getPaymentList();
        for (Payment payment : paymentList
                ) {
            if (payment.getPayMode() == Payment.PAY_MODE_BACKGROUNDPAY) {
                payment.setStatus(OrderService.PAYED);
                paymentService.modifyPayment(payment);
            }

        }
        order.setStatus(OrderService.PAYED);
        orderService.modifyOrder(order);
        payParameter.setBankUrl("/wechat_pay/topay");

        return payParameter;
    }

    @Override
    public Map<String, String> notification(Map<String, String> map) {
        return null;
    }

    @Override
    public PayParameter refund(Order order, Integer price) {
        return null;
    }
}
